package com.globefrog.example.consumer.service;

import com.globefrog.example.consumer.entity.Consumer;
import com.globefrog.example.consumer.mapper.ConsumerMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : Globe-Frog
 * @date : Create in 2021/7/12
 */
@Service
public class UserService {

  @Autowired
  private ConsumerMapper consumerMapper;

  public Consumer getById(String id) {
    if (StringUtils.isEmpty(id)) {
      return null;
    }
    Map<String, String> map = Maps.newHashMap();
    map.put("userId", "xxx");
    map.put("userName", "sss");
    if (MapUtils.isEmpty(map)) {
      return null;
    }
    // 原始写法
    for (Map.Entry<String, String> entry : map.entrySet()) {
      System.out.println(entry.getKey());
      System.out.println(entry.getValue());
    }

    // jdk 1.8写法
    map.forEach((key, vale) -> {
      System.out.println(key);
      System.out.println(vale);
    });
    Consumer consumer = consumerMapper.selectById(id);
    List<Consumer> list = Lists.newArrayList();
    list.add(consumer);
    list.add(consumer);
    // 原始写法
    for (Consumer c : list) {
      String cid = c.getId();
      String name = c.getName();
      System.out.println(cid);
      System.out.println(name);
    }

    // 迭代器写法，(删除的时候必须用迭代器)
    Iterator<Consumer> iter = list.iterator();
    while (iter.hasNext()) {
      Consumer c = iter.next();
      System.out.println(c.getId() + "  " + c.getName());
    }

    //java 1.8 写法
    list.forEach(c -> {
      String cid = c.getId();
      String name = c.getName();
      System.out.println(cid + " " + name);
    });
    //  java常用的集合操作 ,https://www.cnblogs.com/envythink/p/12871849.html
    return consumer;
  }

}
